<?php 
/* 
	$_FILES["file"]["name"] - 被上传文件的名称
	$_FILES["file"]["type"] - 被上传文件的类型
	$_FILES["file"]["size"] - 被上传文件的大小，以字节计
	$_FILES["file"]["tmp_name"] - 存储在服务器的文件的临时副本的名称
	$_FILES["file"]["error"] - 由文件上传导致的错误代码 
*/
	$id = $_GET['id'];
	$type = $_POST['type'];
	$imgName = $_POST['imgName'];
	$description = $_POST['description'];
	switch($type){
		case 'default':
			$typeName = '默认图片';
			break;
		case 'banner':
			$typeName = 'Banner图片';
			break;
		default:
			break;
	}
	include '../common/connect.php';
	if( empty($id) ){	//新图片上传
		$file = $_FILES["file"];
		if( $file['error'] > 0 ){
			$result = 3;	/* 上传失败 */
		}else if( !((($file["type"] == "image/gif") || ($file["type"] == "image/jpeg") || ($file["type"] == "image/pjpeg"))) ){
			$result = 2;	/* 格式错误 */
		}else if( $file["size"] > 200000 ){
			$result = 1;	/* 大小错误 */
		}else{
			$jpg = explode('.',$file['name']);
			$jpg = ".".$jpg[1];
			$size = round($file['size']/1024,2);
			$size = $size.'KB';
			$path = 'upload/'. time() . rand(1,999) . $jpg;
			move_uploaded_file($file["tmp_name"],'../'.$path);
			mysql_query("INSERT INTO pic (path,imgName,type,typeName,description,size) VALUES ( '{$path}','{$imgName}','{$type}','{$typeName}','{$description}','{$size}')");
			$result = 0;	/* 上传成功 */
		}
	}else{	//修改图片信息
		date_default_timezone_set('PRC');	//更改时区
		$time = date('Y-m-d H:i:s');
		mysql_query("UPDATE pic SET imgName = '{$imgName}',type = '{$type}',typeName = '{$typeName}',description = '{$description}',time = '{$time}' WHERE id = '".$id."'");
		$result = 4;	/* 修改成功 */
	}
	mysql_close($con);
	echo '<script type="text/javascript">window.top.window.uploaded('.$result.');</script>';
?>

